<template>
  <div style="min-height: 500px" class="row q-col-gutter-x-md">
    <div class="col-lg-3 col-md-5" style="height:800px;overflow:auto;">
      <q-card>
        <q-card-section>
          <q-tree
            ref="orgTree"
            :nodes="data"
            node-key="id"
            label-key="name"
            :selected.sync="nodeSelected"
            selected-color="primary"
            :expanded.sync="nodeExpanded"
          >
            <template v-slot:header-root="prop">
              <div class="row items-center" style="width:100%;" id="treeName">
                <q-badge transparent align="middle" color="primary">
                  菜单
                </q-badge>
                <div class="text-weight-bold ellipsis q-ml-xs" style="width:80%" :title="prop.node.kjCode + ' - ' + prop.node.name">{{ prop.node.kjCode + ' - ' + prop.node.name }}</div>
              </div>
              <q-popup-proxy context-menu>
                <q-list dense class="text-grey">
                  <!-- <q-item clickable v-close-popup @click="addChild(prop.node)">
                    <q-item-section>
                      <q-item-label>新增平级菜单</q-item-label>
                    </q-item-section>
                  </q-item>
                  <q-item clickable v-close-popup @click="addChild(prop.node)">
                    <q-item-section>
                      <q-item-label>新增下级菜单</q-item-label>
                    </q-item-section>
                  </q-item> -->
                  <q-item clickable v-close-popup @click="edit(prop.node)">
                    <q-item-section>
                      <q-item-label>修改</q-item-label>
                    </q-item-section>
                  </q-item>
                  <!-- <q-item clickable v-close-popup @click="addChild(prop.node)">
                    <q-item-section>
                      <q-item-label>删除</q-item-label>
                    </q-item-section>
                  </q-item> -->
                </q-list>
              </q-popup-proxy>
            </template>
            <template v-slot:header-header="prop">
              <div class="row items-center" style="width:100%;flex-wrap:nowrap">
                <q-badge transparent align="middle" color="orange">
                  tab
                </q-badge>
                <div class="text-weight-bold ellipsis q-ml-xs" style="width:80%" :title="prop.node.name">{{ prop.node.name }}</div>
              </div>
              <!-- <q-popup-proxy context-menu>
                <q-list dense class="text-grey"> -->
                  <!-- <q-item clickable v-close-popup @click="addChild(prop.node)">
                    <q-item-section>
                      <q-item-label>新增tab</q-item-label>
                    </q-item-section>
                  </q-item> -->
                  <!-- <q-item clickable v-close-popup @click="edit(prop.node)">
                    <q-item-section>
                      <q-item-label>修改</q-item-label>
                    </q-item-section>
                  </q-item>
                  <q-item clickable v-close-popup @click="addChild(prop.node)">
                    <q-item-section>
                      <q-item-label>删除</q-item-label>
                    </q-item-section>
                  </q-item>
                </q-list>
              </q-popup-proxy> -->
            </template>
          </q-tree>
        </q-card-section>
      </q-card>
    </div>
    <div class="col-lg-9 col-md-7">
      <q-card>
        <q-card-section>
          <q-tabs
            v-model="step"
            dense
            inline-label
            class="text-grey"
            active-color="primary"
            indicator-color="primary"
            align="left"
            :breakpoint="0"
            narrow-indicator
          >
            <q-tab name="1" icon="fas fa-cube" label="按钮" />
            <q-tab name="2" icon="fas fa-cube" label="字段" />
          </q-tabs>
          <q-separator />
          <q-tab-panels v-model="step" animated>
            <q-tab-panel class="q-pa-none" name="1">
              <q-table
                flat
                dense
                class="bg-white"
                :data="dataButtom"
                :columns="columnsButtom"
                row-key="id"
                separator="cell"
                selection="multiple"
                :selected.sync="selectedButtom"
                :filter="searchFormButtom"
                :pagination.sync="paginationButtom"
                :rows-per-page-options="[40, 50, 60, 80]"
                no-data-label="暂无数据"
                table-class="h-custom"
                @request="getTable"
                :hide-bottom="dataButtom.length!=0"
              >
                <template v-slot:header="props">
                  <q-tr :props="props" style="height:36px">
                    <q-th>
                      <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
                    </q-th>
                    <q-th v-for="col in props.cols" :key="col.name" :props="props">
                      {{ col.label }}
                    </q-th>
                  </q-tr>
                </template>
                <template v-slot:body="props">
                  <q-tr :props="props" style="height:36px">
                    <q-td width="52">
                      <q-checkbox dense v-model="props.selected" />
                    </q-td>
                    <q-td key="name" :props="props">{{ props.row.name }}</q-td>
                    <q-td key="code" :props="props">{{ props.row.code  }}</q-td>
                    <q-td key="createTime" :props="props">{{ props.row.createTime }}</q-td>
                  </q-tr>
                  <q-tr v-show="props.expand" :props="props">
                    <q-td colspan="100%">
                    </q-td>
                  </q-tr>
                </template>
                <template v-slot:pagination="props">
                  <!-- <q-pagination
                    v-model="paginationRole.page"
                    color="blue"
                    :max-pages="5"
                    :max="pagesRole"
                    :boundaryLinks="true"
                    :directionLinks="true"
                  /> -->
                  <q-pagination
                    v-model="paginationButtom.page"
                    :max="pagesButtom"
                    :input="true"
                  >
                  </q-pagination>
                  共 {{ totalButtom }} 条
                </template>
              </q-table>
            </q-tab-panel>
            <q-tab-panel class="q-pa-none" name="2">
               <q-table
                flat
                dense
                class="bg-white"
                :data="dataField"
                :columns="columnsField"
                row-key="id"
                separator="cell"
                selection="multiple"
                :selected.sync="selectedField"
                :filter="searchFormField"
                :pagination.sync="paginationField"
                :rows-per-page-options="[40, 50, 60, 80]"
                no-data-label="暂无数据"
                table-class="h-custom"
                @request="getTable"
                :hide-bottom="dataField.length!=0"
              >
                <template v-slot:header="props">
                  <q-tr :props="props" style="height:36px">
                    <q-th>
                      <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
                    </q-th>
                    <q-th v-for="col in props.cols" :key="col.name" :props="props">
                      {{ col.label }}
                    </q-th>
                  </q-tr>
                </template>
                <template v-slot:body="props">
                  <q-tr :props="props" style="height:36px">
                    <q-td width="52">
                      <q-checkbox dense v-model="props.selected" />
                    </q-td>
                    <q-td key="name" :props="props">{{ props.row.name }}</q-td>
                    <q-td key="code" :props="props">{{ props.row.code  }}</q-td>
                    <q-td key="createTime" :props="props">{{ props.row.createTime }}</q-td>
                  </q-tr>
                  <q-tr v-show="props.expand" :props="props">
                    <q-td colspan="100%">
                    </q-td>
                  </q-tr>
                </template>
                <template v-slot:pagination="props">
                  <q-pagination
                    v-model="paginationField.page"
                    :max="pagesField"
                    :input="true"
                  >
                  </q-pagination>
                  共 {{ totalField }} 条
                </template>
              </q-table>
            </q-tab-panel>
          </q-tab-panels>
          <!-- <q-stepper
            v-model="step"
            vertical
            flat
            color="light-blue"
            active-color="light-blue"
            animated
          >
            <q-step
              :name="1"
              title="按钮"
              icon="fas fa-user-tag"
              active-icon="fas fa-user-tag"
              @click="step = 1"
            >
              <q-table
                flat
                dense
                class="bg-white"
                :data="dataButtom"
                :columns="columnsButtom"
                row-key="id"
                separator="cell"
                selection="multiple"
                :selected.sync="selectedButtom"
                :filter="searchFormButtom"
                :pagination.sync="paginationButtom"
                :rows-per-page-options="[3, 5, 10, 20]"
                no-data-label="暂无数据"
                table-class="h-custom"
                @request="getTable"
              >
                <template v-slot:header="props">
                  <q-tr :props="props" style="height:36px">
                    <q-th>
                      <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
                    </q-th>
                    <q-th v-for="col in props.cols" :key="col.name" :props="props">
                      {{ col.label }}
                    </q-th>
                  </q-tr>
                </template>
                <template v-slot:body="props">
                  <q-tr :props="props" style="height:36px">
                    <q-td width="52">
                      <q-checkbox dense v-model="props.selected" />
                    </q-td>
                    <q-td key="name" :props="props">{{ props.row.name }}</q-td>
                    <q-td key="kjCode" :props="props">{{ props.row.kjCode  }}</q-td>
                    <q-td key="createTime" :props="props">{{ props.row.createTime }}</q-td>
                  </q-tr>
                  <q-tr v-show="props.expand" :props="props">
                    <q-td colspan="100%">
                    </q-td>
                  </q-tr>
                </template>
                <template v-slot:pagination="props"> -->
                  <!-- <q-pagination
                    v-model="paginationRole.page"
                    color="blue"
                    :max-pages="5"
                    :max="pagesRole"
                    :boundaryLinks="true"
                    :directionLinks="true"
                  /> -->
                  <!-- <q-pagination
                    v-model="paginationButtom.page"
                    :max="pagesButtom"
                    :input="true"
                  >
                  </q-pagination>
                  共 {{ totalButtom }} 条
                </template>
              </q-table>
            </q-step>
            <q-step
              :name="2"
              title="字段"
              icon="fas fa-user-tag"
              active-icon="fas fa-user-tag"
              @click="step = 2"
            >
            <q-table
                flat
                dense
                class="bg-white"
                :data="dataField"
                :columns="columnsField"
                row-key="id"
                separator="cell"
                selection="multiple"
                :selected.sync="selectedField"
                :filter="searchFormField"
                :pagination.sync="paginationField"
                :rows-per-page-options="[3, 5, 10, 20]"
                no-data-label="暂无数据"
                table-class="h-custom"
                @request="getTable"
              >
                <template v-slot:header="props">
                  <q-tr :props="props" style="height:36px">
                    <q-th>
                      <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
                    </q-th>
                    <q-th v-for="col in props.cols" :key="col.name" :props="props">
                      {{ col.label }}
                    </q-th>
                  </q-tr>
                </template>
                <template v-slot:body="props">
                  <q-tr :props="props" style="height:36px">
                    <q-td width="52">
                      <q-checkbox dense v-model="props.selected" />
                    </q-td>
                    <q-td key="name" :props="props">{{ props.row.name }}</q-td>
                    <q-td key="kjCode" :props="props">{{ props.row.kjCode  }}</q-td>
                    <q-td key="createTime" :props="props">{{ props.row.createTime }}</q-td>
                  </q-tr>
                  <q-tr v-show="props.expand" :props="props">
                    <q-td colspan="100%">
                    </q-td>
                  </q-tr>
                </template>
                <template v-slot:pagination="props">
                  <q-pagination
                    v-model="paginationField.page"
                    :max="pagesField"
                    :input="true"
                  >
                  </q-pagination>
                  共 {{ totalField }} 条
                </template>
              </q-table>
            </q-step> -->
            <!-- <q-step
              :name="3"
              title="tab页"
              icon="fas fa-user-tag"
              active-icon="fas fa-user-tag"
              @click="step = 3"
            >
            <q-table
                flat
                dense
                class="bg-white"
                :data="dataTable"
                :columns="columnsTable"
                row-key="id"
                separator="cell"
                selection="multiple"
                :selected.sync="selectedTable"
                :filter="searchFormTable"
                :pagination.sync="paginationTable"
                :rows-per-page-options="[3, 5, 10, 20]"
                no-data-label="暂无数据"
                table-class="h-custom"
              >
                <template v-slot:header="props">
                  <q-tr :props="props" style="height:36px">
                    <q-th>
                      <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
                    </q-th>
                    <q-th v-for="col in props.cols" :key="col.name" :props="props">
                      {{ col.label }}
                    </q-th>
                  </q-tr>
                </template>
                <template v-slot:body="props">
                  <q-tr :props="props" style="height:36px">
                    <q-td width="52">
                      <q-checkbox dense v-model="props.selected" />
                    </q-td>
                    <q-td key="roleName" :props="props">{{ props.row.roleName }}</q-td>
                    <q-td key="roleCode" :props="props">{{ props.row.roleCode  }}</q-td>
                    <q-td key="createTime" :props="props">{{ props.row.createTime }}</q-td>
                  </q-tr>
                  <q-tr v-show="props.expand" :props="props">
                    <q-td colspan="100%">
                    </q-td>
                  </q-tr>
                </template>
                <template v-slot:pagination="props">
                  <q-pagination
                    v-model="paginationTable.page"
                    :max="pagesTable"
                    :input="true"
                  >
                  </q-pagination>
                  共 {{ totalTable }} 条
                </template>
              </q-table>
            </q-step> -->
          <!-- </q-stepper> -->
        </q-card-section>
      </q-card>
      <q-dialog
        v-model="addDialog"
        persistent
        :maximized="maximizedToggle"
        transition-show="slide-up"
        transition-hide="slide-down">
        <q-card class="bg-white relative-position" style="min-width: 40vw">
          <q-bar class="bg-white q-py-lg">
            <strong>{{ addOrEdit === 1? '新增' : '修改'}}</strong>
            <q-space />
            <q-btn dense flat icon="minimize" @click="maximizedToggle = false" :disable="!maximizedToggle">
              <q-tooltip v-if="maximizedToggle" content-class="bg-white text-primary">最小化</q-tooltip>
            </q-btn>
            <q-btn dense flat icon="crop_square" @click="maximizedToggle = true" :disable="maximizedToggle">
              <q-tooltip v-if="!maximizedToggle" content-class="bg-white text-primary">最大化</q-tooltip>
            </q-btn>
            <q-btn dense flat icon="close" @click="addDialog = false">
              <q-tooltip content-class="bg-white text-primary">关闭</q-tooltip>
            </q-btn>
          </q-bar>
          <q-separator />
          <q-card-section :style="maximizedToggle?'margin-bottom:53px;max-height:83.5vh': 'margin-bottom:53px;min-height:50vh;max-height:60vh'" class="scroll">
            <div class="row q-col-gutter-sm">
              <div class="col-12"  v-if="addOrEditForm.type == 0">
                <div :class="$v.addOrEditForm.kjCode.$error?'text-negative': ''">编码</div>
                <q-input
                  v-model="addOrEditForm.kjCode"
                  error-message="该字段为必填项"
                  :error="$v.addOrEditForm.kjCode.$error"
                  @blur="$v.addOrEditForm.kjCode.$touch"
                  dense hint/>
              </div>
              <div class="col-12">
                <div :class="$v.addOrEditForm.name.$error?'text-negative': ''">名称</div>
                <q-input
                  v-model="addOrEditForm.name"
                  error-message="该字段为必填项"
                  :error="$v.addOrEditForm.name.$error"
                  @blur="$v.addOrEditForm.name.$touch"
                  dense hint/>
              </div>
            </div>
          </q-card-section>
          <q-separator class="absolute" style="bottom: 52px; left: 0"/>
          <q-card-actions align="right" class="absolute-bottom bg-white">
            <q-btn flat label="取消" color="primary" v-close-popup/>
            <q-btn flat label="保存" color="primary" :loading="loadingSave" @click="saveHeader"/>
          </q-card-actions>
        </q-card>
      </q-dialog>
    </div>
  </div>
</template>

<script>
import { transformToTreeFormat } from 'src/utils/fns'
import { required } from 'vuelidate/lib/validators'
// import LeftMenu from './LeftMenu'
export default {
  name: 'Menu',
  props: {
    menudata: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      // 弹框
      addOrEdit: 2,
      loadingSave: false,
      addDialog: false,
      maximizedToggle: false,
      step: '1',
      data: [],
      nodeSelected: [],
      nodeExpanded: [],
      nodeTicked: [],
      ids: [],
      // 新增/修改
      addOrEditForm: {
        id: '',
        kjCode: '',
        name: ''
      },
      dataField: [],
      searchFormField: {
        roleName: '',
        roleCode: '',
        createTime: ''
      },
      totalField: '',
      selectedField: [],
      paginationField: {
        page: 1,
        rowsPerPage: 40,
        rowsNumber: 5,
        sortBy: '',
        descending: Boolean
      },
      dataButtom: [],
      searchFormButtom: {
        roleName: '',
        roleCode: '',
        createTime: ''
      },
      totalButtom: '',
      selectedButtom: [],
      paginationButtom: {
        page: 1,
        rowsPerPage: 40,
        rowsNumber: 5,
        sortBy: '',
        descending: Boolean
      },
      dataTable: [],
      searchFormTable: {
        roleName: '',
        roleCode: '',
        createTime: ''
      },
      totalTable: '',
      tableId: '',
      selectedTable: [],
      paginationTable: {
        page: 1,
        rowsPerPage: 5,
        rowsNumber: 5,
        sortBy: '',
        descending: Boolean
      },
      columnsButtom: [
        { name: 'name', required: true, label: '按钮名称', align: 'left', field: 'name', sortable: true },
        { name: 'code', required: true, label: '按纽编码', align: 'left', field: 'code', sortable: true },
        { name: 'createTime', field: 'createTime', align: 'left', label: '创建时间', sortable: true }
      ],
      columnsTable: [
        { name: 'name', required: true, label: 'Tab页名称', align: 'left', field: 'name', sortable: true },
        { name: 'code', required: true, label: 'Tab页编码', align: 'left', field: 'code', sortable: true },
        { name: 'createTime', field: 'createTime', align: 'left', label: '创建时间', sortable: true }
      ],
      columnsField: [
        { name: 'name', required: true, label: '字段名称', align: 'left', field: 'name', sortable: true },
        { name: 'code', required: true, label: '字段编码', align: 'left', field: 'code', sortable: true },
        { name: 'createTime', field: 'createTime', align: 'left', label: '创建时间', sortable: true }
      ]
    }
  },
  created () {
    this.getMenu()
  },
  watch: {
    'step' () {
      if (this.tableId.length !== 0) {
        this.getTable()
      }
    },
    'nodeSelected' () {
      console.log(this.nodeSelected)
      if (this.nodeSelected !== null) {
        this.tableId = this.nodeSelected
        this.getTable()
        console.log(this.$refs.orgTree)
      }
    }
  },
  computed: {
    pagesButtom () {
      return Math.ceil(this.totalButtom / this.paginationButtom.rowsPerPage)
    },
    pagesField () {
      return Math.ceil(this.totalField / this.paginationField.rowsPerPage)
    }
  },
  validations: {
    addOrEditForm: {
      name: { required },
      kjCode: { required }
    }
  },
  methods: {
    edit (props) {
      this.addOrEditForm = {
        type: props.type,
        name: props.name,
        kjCode: props.kjCode,
        id: props.id
      }
      this.addOrEdit = 2
      this.addDialog = true
    },
    async saveHeader () {
      this.loadingSave = true
      await this.$api({
        method: 'put',
        url: '/mdm/permission/update',
        params: this.addOrEditForm,
        emulateJSON: true,
        useLoadLayer: true,
        useNotify: true,
        successCallback: ({ data }) => {
          this.addDialog = false
          console.log(data)
          this.getMenu()
        },
        errorCallback (err) {
          console.log('未处理的业务级error：', err)
        }
      })
      this.loadingSave = false
    },
    getTable () {
      let type = ''
      if (this.step === '1') {
        type = 1
      } else {
        type = -1
      }
      this.$api({
        method: 'post',
        url: `/mdm/permission/query`,
        params: {
          typeIds: type,
          parentId: this.tableId
        },
        emulateJSON: true,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          console.log(this.step)
          if (this.step === '1') {
            this.dataButtom = data
            console.log(this.dataField)
          } else {
            this.dataField = data
          }
        },
        errorCallback (err) {
          console.log('未处理的业务级error：', err)
        }
      })
    },
    getMenu () {
      this.data = []
      // let arr = [{typeIds: 0}]
      //   typeIds: 0,
      //   typeIds: 2
      // }
      this.$api({
        method: 'post',
        url: '/mdm/permission/query',
        params: {
          typeIds: '0,2'
        },
        emulateJSON: true,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          data.forEach(item => {
            this.ids.push(item.id)
            if (item.type === 0) {
              item.header = 'root'
            } else if (item.type === 2) {
              item.header = 'header'
            }
            // item.name = String(item.code) + String(item.name)
          })
          this.nodeExpanded = this.ids
          this.data = transformToTreeFormat(data, 'id', 'parentId', 'children')
          console.log(this.data)
        },
        errorCallback (err) {
          console.log('未处理的业务级error：', err)
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
#treeName
  flex-wrap nowrap
</style>
